package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.HdfsTableService;
import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.model.rock.HdfsTableEntity;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/hdfs_table")
public class HdfsTableController2 {
    @Autowired
    private HdfsTableService hdfsTableService;

    @Data
    public static class Params {
        public String ptdate;
        public Long id;

        public String clusterName;
        public String dbName;
        public String queue;
        public String accessID;
        public String appid;
    }

    @RestGrpcMapping("/listHdfsTable.do")
         public NTSTATUS listHdfsTable(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            List<HdfsTableEntity> res = hdfsTableService.listHdfsTable(aa.clusterName);
            log.info("success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getHdfsTable.do")
    public NTSTATUS getHdfsTable(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            HdfsTableEntity res = hdfsTableService.getHdfsTable(aa.id);
            log.info("success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/deleteHdfsTable.do")
    public NTSTATUS deleteHdfsTable(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            //            aa.ptdate = "2021-06-01";
            Boolean res = hdfsTableService.deleteHdfsTable(aa.clusterName, aa.queue, aa.accessID, aa.appid);
            log.info("success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
